/**
 * Created by Administrator on 2017/8/29 0029.
 */
operateFormatter = function (value, row, index) {
    if (row.picture != null || row.picture != "") {
        return [
            '<a class="edit" href="javascript:void(0)">更换图片</a>'+
                '&nbsp;&nbsp;&nbsp;&nbsp;'+
            '<a class="remove" href="javascript:void(0)" style="color: red">删除</a>'
        ].join('');
    }else {
        return [
            '<a class="remove" href="javascript:void(0)" style="color: red">删除</a>'
        ].join('');
    }
};

window.operateEvents = {
    'click .remove': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/management/updateIsRecommend'), {
                id: row.id
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    },
    'click .edit': function (e, value, row) {
        showModel("updatePic-div",function () {
            $("#id").val(row.id);
            $("#pic").attr("src",  File_Download_Url + "/" + row.picture);
            $("#pic").attr("img-path", row.picture);
        })
    }
};

storePic = function (value, row, index) {
    var img = row.headPic;
    var imgUrl = "";
    if (img != null && img != '') {
        imgUrl = File_Download_Url + img;
        return [
            '<image src=' + imgUrl + ' style="width:80px;height:80px" onclick="enlargeImage(this)"/>'
        ].join('');
    } else {
        return [
            '<image src=' + $().AbsUrlParse("/static/images/no.png") + ' style="width:80px;height:80px" onclick="enlargeImage(this)"/>'
        ].join('');
    }
};

function queryBusinessName() {
    var name = $("#name").val();
    var cityName = $("#cityName").val();
    var url = $().AbsUrlParse("/web/management/queryRecommendBusiness?businessName=" + name + "&name=" + cityName);
    var table = $("#table");
    table.bootstrapTable("refresh", {url: url});
}

sorting = function (value, row, index) {
    return "<a href='javascript:void(0)' onclick='updateBusSorting(" + row.sorting + "," + row.id + ")'><span>" + row.sorting + "</span></a>";
};

/**
 * 点击排名事件
 * @param orderNum
 * @param id
 */
var updateBusSorting = function (sorting, id) {
    showModel("showBusSorting-div", function () {
        $("#pid").val(id);
        $("#sorting").val(sorting);
    });
};

/**
 * 判断输入是否为数字
 * @param v
 * @returns {boolean}
 */
function isNumber(v) {
    var str = v.val();
    var reg = parseFloat(str);
    var re = /^\+?[0-9][0-9]*$/;
    if (!re.test(str)) {
        return false;
    }
    return !isNaN(reg);
}

/**
 * 修改轮播推荐排名按钮点击事件
 */
bindClick("button[data-event=merge-sortingUpdate]", function () {
    var form = $("#updateBusSortingFrom");
    if (!form.valid()) {
        return false;
    }
    var sorting = $("#sorting");
    if (!isNumber(sorting)) {
        hint("请填入非负的正整数.");
        return false;
    }
    var params = form.formToArray();
    $.post($().AbsUrlParse('/web/management/updateOrderNum'), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            //关闭model方法
            closeModel('showBusSorting-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

/**
 * 修改轮播推荐排名按钮点击事件
 */
bindClick("button[data-event=submit-updatePic]", function () {
    var pic = $("#pic").attr("img-path");
    $("#picPath").val(pic);

    var form = $("#updatePicFrom");
    if (!form.valid()) {
        return false;
    }

    var params = form.formToArray();
    $.post($().AbsUrlParse('/web/management/updatePic'), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('更换成功');
            //关闭model方法
            closeModel('updatePic-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

function enlargeImage(value) {
    showModel("detailImage-div", function () {
        var imgs = '<image src=' + value.src + ' style="height:500px" />';
        $("#image").html(imgs);
    })
}
